package bingo.component.dhtmlxgrid.config;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import bingo.component.amchart.graphics.BaseGraphic;

/**
 * config配置头节点信息 Jul 29, 2008 11:20:53 AM
 * Author:yangdongyu(yangdy@bingosoft.net) ReadMe: ChangeLog:
 */
public class Head extends BaseGraphic {
	private List<Column> columns = new ArrayList<Column>();
	private Settings settings;
	private BeforeInit beforeInit;
	private AfterInit afterInit;

	public Settings getSettings() {
		return settings;
	}

	public void setSettings(Settings settings) {
		this.settings = settings;
	}

	public List<Column> getColumns() {
		return columns;
	}

	public void setColumns(List<Column> columns) {
		this.columns = columns;
	}

	public GraphicType getGraphicType() {
		return null;
	}

	public boolean isRootElement() {
		return true;
	}

	public void addColumn(Column column) {
		columns.add(column);
	}

	public void delColumn(Column column) {
		int columnIndex = columns.indexOf(column);
		columns.remove(column);

		if (this.afterInit == null) return;
		
		List<Call> calls = this.afterInit.getCalls();
		for (Call call : calls.toArray(new Call[calls.size()])) {
			if ("attachHeader".equalsIgnoreCase(call.getCommand())
					|| "attachFooter".equalsIgnoreCase(call.getCommand())
					|| "attachFooterByEnv".equalsIgnoreCase(call.getCommand())) {
				String[] columnIds = StringUtils.split(call.getParams().get(0)
						.getValue(), ",");
				String paramValue = "";
				boolean headerValid = false;
				for (int i = 0; i < columnIds.length; i++) {
					if (i != columnIndex) {
						paramValue += "," + columnIds[i];
						if (!headerValid) {
							if (!"#cspan".equalsIgnoreCase(columnIds[i])
									&& !"#rspan".equalsIgnoreCase(columnIds[i])) {
								headerValid = true;
							}
						}
					}
				}
				call.getParams().get(0).setValue(
						StringUtils.substring(paramValue, 1));
				if (!headerValid)
					this.afterInit.delCall(call);
			}
		}
	}

	public void delColumnsByColumnIds(String columnIds) {
		String[] columns = StringUtils.split(columnIds, ",");
		for (String column : columns) {
			delColumn(new Column(column));
		}
	}

	public void delColumnsByRetainColumnIds(String columnIds) {
		String[] retainColumns = StringUtils.split(columnIds, ",");
		Column[] columns = this.columns
				.toArray(new Column[this.columns.size()]);
		for (Column column : columns) {
			boolean find = false;
			for (String retainColumn : retainColumns) {
				if (column.getId().equalsIgnoreCase(retainColumn)) {
					find = true;
					break;
				}
			}
			if (!find)
				delColumn(column);
		}
	}

	public BeforeInit getBeforeInit() {
		return beforeInit;
	}

	public void setBeforeInit(BeforeInit beforeInit) {
		this.beforeInit = beforeInit;
	}

	public AfterInit getAfterInit() {
		return afterInit;
	}

	public void setAfterInit(AfterInit afterInit) {
		this.afterInit = afterInit;
	}
}
